package com.meilingcloud.research.controller;

import com.meilingcloud.common.annotation.Log;
import com.meilingcloud.common.core.controller.BaseController;
import com.meilingcloud.common.core.domain.AjaxResult;
import com.meilingcloud.common.core.page.TableDataInfo;
import com.meilingcloud.common.enums.BusinessType;
import com.meilingcloud.common.utils.SecurityUtils;
import com.meilingcloud.research.domain.RwsFollow;
import com.meilingcloud.research.service.IRwsFollowService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Arrays;
import java.util.List;

/**
 * 随访列表Controller
 *
 * @author chenl
 * @date 2021-04-21
 */
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/research/follow")
public class RwsFollowController extends BaseController {

    private final IRwsFollowService iRwsFollowService;

    /**
     * 查询随访列表列表
     */
    @PreAuthorize("@ss.hasPermi('research:follow:list')")
    @GetMapping("/list")
    public TableDataInfo list(RwsFollow rwsFollow) {
        startPage();
        List<RwsFollow> list = iRwsFollowService.queryList(rwsFollow);
        List<RwsFollow> rwsFollows = iRwsFollowService.handleFollowList(list);
        return getDataTable(rwsFollows);
    }

    /**
     * 获取待随访数量
     */
    @GetMapping("/toFollowCount")
    public AjaxResult getToFollowCount() {
        return AjaxResult.success(iRwsFollowService.getToFollowCount());
    }

    /**
     * 获取随访列表详细信息
     */
    @PreAuthorize("@ss.hasPermi('research:follow:query')")
    @GetMapping(value = "/{followId}")
    public AjaxResult getInfo(@PathVariable("followId") Long followId) {
        return AjaxResult.success(iRwsFollowService.getFollow(followId));
    }

    /**
     * 新增随访列表
     */
    @PreAuthorize("@ss.hasPermi('research:follow:add')")
    @Log(title = "随访列表 ", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Valid @RequestBody RwsFollow rwsFollow) {
        rwsFollow.setCreateBy(SecurityUtils.getUsername());
        return toAjax(iRwsFollowService.saveFollow(rwsFollow));
    }

    /**
     * 修改随访列表
     */
    @PreAuthorize("@ss.hasPermi('research:follow:edit')")
    @Log(title = "随访列表 ", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Valid @RequestBody RwsFollow rwsFollow) {
        rwsFollow.setUpdateBy(SecurityUtils.getUsername());
        return toAjax(iRwsFollowService.updateFollowById(rwsFollow));
    }

    @PreAuthorize("@ss.hasPermi('research:follow:edit')")
    @Log(title = "随访列表 ", businessType = BusinessType.UPDATE)
    @PostMapping("/updateBatch")
    public AjaxResult editBatch(@RequestBody List<RwsFollow> rwsFollows) {
        return toAjax(iRwsFollowService.updateFollowBatch(rwsFollows));
    }

    /**
     * 删除随访列表
     */
    @PreAuthorize("@ss.hasPermi('research:follow:remove')")
    @Log(title = "随访列表 ", businessType = BusinessType.DELETE)
    @DeleteMapping("/{followIds}")
    public AjaxResult remove(@PathVariable Long[] followIds) {
        return toAjax(iRwsFollowService.removeByIds(Arrays.asList(followIds)));
    }

    @GetMapping("/statisticFollow")
    public AjaxResult statisticFollow() {
        return AjaxResult.success(iRwsFollowService.statisticFollow());
    }
}
